package com.rahwi.rahwi_server.Controller;

import com.rahwi.rahwi_server.Entity.User;
import com.rahwi.rahwi_server.Service.impl.UserService;
import com.rahwi.rahwi_server.Utils.Converter.ICopy;
import com.rahwi.rahwi_server.Utils.Converter.UserConverter;
import com.rahwi.rahwi_server.vo.data.BaseUser;
import com.rahwi.rahwi_server.vo.data.IUser;
import com.rahwi.rahwi_server.vo.results.ActionResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){ return "springboot 访问成功! name=" +",age=";}

    @Autowired
    private UserService userService;
    @RequestMapping("/authorize")
    public ActionResult authorizeUserByToken(@RequestHeader("Authorization") String token){
        User user = userService.getUserByToken(token);
        if(user == null){
            return ActionResult.fail(401,"未登录用户或用户不存在，请重新登录");
        }

        else return ActionResult.success(UserConverter.toVoUser(user, (ICopy<BaseUser, User>) user1 -> {
            BaseUser baseUser = new BaseUser();
            BeanUtils.copyProperties(user1,baseUser);
            return baseUser;
        }));
    }

    @RequestMapping("/userInfo/{uid}")
    public ActionResult getUserInfoById(@PathVariable("uid") int uid){
        User user = userService.getUserById(uid);
        if(user == null){
            return ActionResult.fail(401,"用户不存在");
        }
        else return ActionResult.success(UserConverter.toVoUser(user, (ICopy<BaseUser, User>) user1 -> {
            BaseUser baseUser = new BaseUser();
            BeanUtils.copyProperties(user1,baseUser);
            baseUser.setPostCount(user1.getPosts().size());
            return baseUser;
        }));
    }


}
